package com.hethongviet.job.pages;

import org.json.JSONException;
import org.json.JSONObject;

import com.hethongviet.job.AppMain;
import com.hethongviet.job.JobTrackingActivity;
import com.hethongviet.job.R;
import com.hethongviet.job.helpers.JSONAttributes;
import com.hethongviet.job.helpers.JSONHelper;
import com.hethongviet.job.helpers.ListItemProjectAdapter;
import com.hethongviet.job.helpers.ListItemTaskAdapter;
import com.hethongviet.job.helpers.ServiceHelper;
import com.hethongviet.job.helpers.TabMenu;
import com.hethongviet.job.models.Tab;
import com.hethongviet.job.models.Task;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends Activity
{
	private ListView _listViewProjects = null;
	private ListView _listViewTasks = null;
	private LinearLayout _linearLayoutProjects = null;
	private LinearLayout _linearLayoutTasks = null;
	private TabMenu _tabMenu = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		AppMain.makeFullScreen(this);
		setContentView(R.layout.page_main);

		final Button buttonTabProject = (Button) findViewById(R.id.buttonTabProject);
		final Button buttonTabTask = (Button) findViewById(R.id.buttonTabTask);

		final Button buttonUsername = (Button) findViewById(R.id.buttonUsername);
		buttonUsername.setText(AppMain._user.getUsername());
		buttonUsername.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				/* Logout */
				Intent intent = new Intent(MainActivity.this,
						JobTrackingActivity.class);
				startActivity(intent);
			}
		});

		_listViewProjects = (ListView) findViewById(R.id.listViewProjects);
		_listViewTasks = (ListView) findViewById(R.id.listViewTasks);
		_linearLayoutProjects = (LinearLayout) findViewById(R.id.layoutListProjects);
		_linearLayoutTasks = (LinearLayout) findViewById(R.id.layoutListTasks);

		/* Process for tab menu */
		Tab[] tabs = new Tab[2];
		tabs[0] = new Tab();
		tabs[0].setButton(buttonTabProject);
		tabs[0].setButtonFunctions(null);// softkeys
		tabs[0].setLinearLayout(_linearLayoutProjects);
		tabs[1] = new Tab();
		tabs[1].setButton(buttonTabTask);
		tabs[1].setButtonFunctions(null);// softkeys
		tabs[1].setLinearLayout(_linearLayoutTasks);

		_tabMenu = new TabMenu(tabs, R.drawable.button_tab_2,
				R.drawable.button_tab_highlight_2);
		/* List of projects */
		if (AppMain._projectsList.size() != 0)
		{
			_listViewProjects.setAdapter(new ListItemProjectAdapter(
					MainActivity.this, AppMain._projectsList));
			_listViewProjects.setTextFilterEnabled(true);
			_listViewProjects.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id)
				{
					AppMain._currentProject = AppMain._projectsList.get(position);
					if (AppMain._currentProject != null)
					{
						/* Move to tab task */
						_tabMenu.moveToTab(1);
					}
				}
			});
		}
		
		
		/* List tasks */
		new Thread(new Runnable() {
			public void run()
			{
				/* Send data to get the all tasks */
				/* Get list of projects */
				String resultTasksString = ServiceHelper.doGetListOfAllTasks(
						AppMain._user.getUsername(),
						AppMain._user.getMemberId(),
						AppMain._user.isManagerJobs());
				
				// Temporary
				if (resultTasksString == null
						|| resultTasksString.equalsIgnoreCase("")
						|| resultTasksString.charAt(0) != '{')
				{
					AppMain.alert(MainActivity.this,
							getString(R.string.textError),
							getString(R.string.textErrorNetwork));
					return;
				}
				//
				String[] jsonStrings = resultTasksString.split("&");
				AppMain._tasksList.clear();
				JSONObject jsonObject;
				for (int i = 0; i < jsonStrings.length; i++)
				{
					Log.d("Debug", "Task index " + i);
					try
					{
						jsonObject = new JSONObject(jsonStrings[i]);
					} catch (JSONException e)
					{
						e.printStackTrace();
						AppMain.alert(MainActivity.this,
								getString(R.string.textError),
								getString(R.string.textErrorNetwork));
						return;
					}
					Task task = new Task();
					task.setCode(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_CODE));
					task.setDateActualFinish(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_DATE_ACTUAL_FINISH));
					task.setDateActualStart(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_DATE_ACTUAL_START));
					task.setDateCreated(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_DATE_CREATE));
					task.setDateRequestFinish(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_DATE_REQUEST_FINISH));
					task.setDateRequestStart(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_DATE_REQUEST_START));
					task.setDescription(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_DECRIPTION));
					task.setId(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_ID));
					task.setImportantLevel(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_IMPORTANT_LEVEL));
					// task.set...(JSONHelper.getString(jsonObject,
					// JSONAttributes.TASK_LATITUDE));
					task.setWorker(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_MEMBER_ASSIGN));
					task.setName(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_NAME));
					task.setNote(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_NOTE));
					task.setProjectCode(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_PROJECT_CODE));
					task.setProjectId(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_PROJECT_ID));
					task.setProjectName(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_PROJECT_NAME));
					task.setStatus(JSONHelper.getString(jsonObject,
							JSONAttributes.TASK_STATUS));

					AppMain._tasksList.add(task);

				}
				/* Show the list of tasks */
				if (AppMain._tasksList.size() != 0)
				{
					_listViewTasks.setAdapter(new ListItemTaskAdapter(
							MainActivity.this, AppMain._tasksList));
					_listViewTasks.setTextFilterEnabled(true);
					_listViewTasks.setOnItemClickListener(new OnItemClickListener() {
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id)
						{
							AppMain._currentTask = AppMain._tasksList.get(position);
							if (AppMain._currentTask != null)
							{
								Intent intent = new Intent(MainActivity.this,
										DetailTaskActivity.class);
								startActivity(intent);
							}
						}
					});
				}
			}
		}).start();
	}
}
